// ignore_for_file: constant_identifier_names

import '../util.dart';

part 'date_format.dart';

class DateUtil {
  static const num ONE_MINUTE = 60000;
  static const num ONE_HOUR = 3600000;
  static const num ONE_DAY = 86400000;
  static const num ONE_WEEK = 604800000;

  static const String ONE_SECOND_AGO = "秒前";
  static const String ONE_MINUTE_AGO = "分钟前";
  static const String ONE_HOUR_AGO = "小时前";
  static const String ONE_DAY_AGO = "天前";
  static const String ONE_MONTH_AGO = "月前";
  static const String ONE_YEAR_AGO = "年前";

//时间转换
  static String format(DateTime date) {
    num delta =
        DateTime.now().millisecondsSinceEpoch - date.millisecondsSinceEpoch;
    if (delta < 1 * ONE_MINUTE) {
      num seconds = _toSeconds(delta);
      return (seconds <= 0 ? 1 : seconds).toInt().toString() + ONE_SECOND_AGO;
    }
    if (delta < ONE_HOUR) {
      num minutes = _toMinutes(delta);
      return (minutes <= 0 ? 1 : minutes).toInt().toString() + ONE_MINUTE_AGO;
    }
    if (delta < 24 * ONE_HOUR) {
      num hours = _toHours(delta);
      return (hours <= 0 ? 1 : hours).toInt().toString() + ONE_HOUR_AGO;
    }
    if (delta < 48 * ONE_HOUR) {
      return "昨天";
    }
    if (delta < 3 * 24 * ONE_HOUR) {
      return "2$ONE_DAY_AGO";
    }

    return DateFormat.format(date, DateFormatType.ymd);
  }

  static int _toSeconds(num date) {
    return (date / 1000).ceil();
  }

  static int _toMinutes(num date) {
    return (_toSeconds(date) / 60).ceil();
  }

  static int _toHours(num date) {
    return (_toMinutes(date) / 60).ceil();
  }

  static int _toDays(num date) {
    return (_toHours(date) / 24).ceil();
  }

  static String getCurrWeekDay(DateTime date) {
    switch (date.weekday) {
      case 0:
      case 7:
        return '日';
      case 1:
        return '一';
      case 2:
        return '二';
      case 3:
        return '三';
      case 4:
        return '四';
      case 5:
        return '五';
      case 6:
        return '六';
      default:
        return '';
    }
  }

  // 获取当前月份天数
  static int getCurrentDays({
    required int year,
    required int month,
  }) {
    if (month == 2) {
      if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
        return 29;
      } else {
        return 28;
      }
    } else if (month == 1 ||
        month == 3 ||
        month == 5 ||
        month == 7 ||
        month == 8 ||
        month == 10 ||
        month == 12) {
      return 31;
    } else {
      return 30;
    }
  }
}

extension DateTimeEx on DateTime {
  DateTime after({
    int year = 0,
    int month = 0,
    int day = 0,
    int hour = 0,
    int minute = 0,
    int second = 0,
  }) {
    return DateTime(
      this.year + year,
      this.month + month,
      this.day + day,
      this.hour + hour,
      this.minute + minute,
      this.second + second,
    );
  }

  static DateTime quarterSmall(int year, int quarter) {
    return DateTime(year, quarterMonth(quarter));
  }

  int get quarter {
    switch (month) {
      case 1:
      case 2:
      case 3:
        return 1;
      case 4:
      case 5:
      case 6:
        return 2;
      case 7:
      case 8:
      case 9:
        return 3;
      case 10:
      case 11:
      case 12:
        return 4;
      default:
        return 1;
    }
  }

  static int quarterMonth(quarter) {
    switch (quarter) {
      case 1:
        return 1;
      case 2:
        return 4;
      case 3:
        return 7;
      case 4:
        return 10;
      default:
        return 1;
    }
  }

  String get quarterBig {
    switch (quarter) {
      case 1:
        return '一';
      case 2:
        return '二';
      case 3:
        return '三';
      case 4:
        return '四';
      default:
        return '';
    }
  }
}
